﻿using BootstrapBlazor.Components;
using CodeManDesktopBlazor.BlazorComponents.Models;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.Extensions.Localization;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Threading.Tasks;

using Microsoft.Extensions.Logging;


namespace CodeManDesktopBlazor.BlazorComponents.Shared
{
    public partial class MainLayout : LayoutComponentBase, IDisposable
    {
        #region inject

        [Inject]
        private IJSRuntime JSRuntime { get; set; }

        [Inject]
        [NotNull]
        private IStringLocalizer<MainLayout>? Localizer { get; set; }


        [Inject]
        ILogger<MainLayout> Logger { get; set; }

        [Inject]
        NavigationManager NavigationManager { get; set; }


        #endregion
        private bool UseTabSet { get; set; } = true;

        private bool _UseZhSet = true;
        private bool UseZhSet
        {
            get
            {
                return _UseZhSet;
            }

            set
            {
                _UseZhSet = value;
            }
        }

        private string Theme { get; set; } = "";

        private bool IsOpen { get; set; }

        private bool IsFixedHeader { get; set; } = true;

        private bool IsFixedFooter { get; set; } = true;

        private bool IsFullSide { get; set; } = true;

        private bool ShowFooter { get; set; } = true;

        private List<MenuItem> Menus { get; set; }

        private Dictionary<string, string> TabItemTextDictionary { get; set; } 
       
        /// <summary>
        /// OnInitialized 方法
        /// </summary>
        protected override async Task OnInitializedAsync()
        {
            await base.OnInitializedAsync();

            // 菜单获取可以通过数据库获取，此处为示例直接拼装的菜单集合
            TabItemTextDictionary = new()
            {
                [""] = "Index"
            };
            Menus = GetIconSideMenuItems();

            NavigationManager.LocationChanged += HandleLocationChanged;
        }

        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            await base.OnAfterRenderAsync(firstRender);
            if (firstRender)
            {
                try
                {
                    await Task.Delay(100);
                    var jsmain = await GetMainHeight();
                    GData.mainjs = jsmain;
                }
                catch (Exception ex)
                {
                    await Task.Delay(1000);
                    var jsmain = await GetMainHeight();
                    GData.mainjs = jsmain;
                }
            }
        }

        private List<MenuItem> GetIconSideMenuItems()
        {
            var menus = new List<MenuItem>
            {
                //new MenuItem() { Text = "返回组件库", Icon = "fa fa-fw fa-home", Url = "https://www.blazor.zone/components" },
                new MenuItem() { Text =  Localizer["Index"], Icon = "fa fa-fw fa-fa", Url = "/" , Match = NavLinkMatch.All},
                new MenuItem() { Text = Localizer["CodeGens"], Icon = "fa fa-fw fa-free-code-camp", Url = "CodeGens" }, 
                new MenuItem() { Text = Localizer["ClassGenDTOTable"], Icon = "fa fa-fw fa-database", Url = "classGenTable" }, 
                new MenuItem() { Text = Localizer["ConfigSet"], Icon = "fa fa-fw fa-cogs", Url = "appConfigSets" }, 
                //new MenuItem() { Text = Localizer["codeGenTabDemoTab"], Icon = "fa fa-fw fa-database", Url = "codeGenTabDemoTab" }, 
                //new MenuItem() { Text = Localizer["Table"], Icon = "fa fa-fw fa-table", Url = "table" }
            };

            return menus;
        }

        public async Task<MainJs> GetMainHeight()
        {
            var jsmain = await JSRuntime.InvokeAsync<MainJs>("$.getMainProp");
            return jsmain as MainJs;
        }
        private async Task GetCultureAsync()
        {
            LangFlagenum cultureName = UseZhSet ? LangFlagenum.zh : LangFlagenum.en;
            GData.SetDefaultThreadCurrentCulture(cultureName);
            GData.DefaultThreadCurrentCulture = cultureName;
            await Task.Delay(10);
        }

        private void HandleLocationChanged(object sender, LocationChangedEventArgs e)
        {
            Logger.LogInformation("URL of new location: {Location}", e.Location);
        }

        public void Dispose()
        {
            NavigationManager.LocationChanged -= HandleLocationChanged;
        }
    }
}
